package com.lzy.cloud.order.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.order.dtos.BaseOrderDto;
import com.lzy.cloud.order.dtos.OrderInfoDto;
import com.lzy.cloud.order.dtos.rvcar.CarRentOrderDto;
import com.lzy.cloud.order.dtos.rvcar.CarSaleOrderDto;
import com.lzy.cloud.order.facade.OrderInfoFacade;
import com.lzy.cloud.order.remote.BaseApi;
import com.lzy.cloud.order.request.OrderInfoCarRentRequest;
import com.lzy.cloud.order.request.OrderInfoRequest;
import com.lzy.cloud.order.response.MemberToken;
import com.lzy.cloud.order.response.OrderInfoResponse;
import com.lzy.cloud.order.service.OrderInfoService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.core.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 订单总表
 * Created by WIN7 on 2018/6/6.
 */
@RestController
@RequestMapping("/order")
public class OrderInfoController {

    @Autowired
    private OrderInfoFacade orderInfoFacade;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private BaseApi baseApi;

    /**
     * 查询订单列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result queryList(@RequestBody OrderInfoRequest request) {
        Ensure.that(request).isNotNull("00000002");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<OrderInfoDto> pageInfo = orderInfoService.queryList(pager,DozerUtils.map(request,OrderInfoDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), OrderInfoResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 方法说明：根据订单号查找订单
     * @param   orderId
     * @return      
     * @date        2018/7/10 10:34
     */
    @RequestMapping(value = "/find/{orderId}",method = RequestMethod.GET)
    public Result find(@PathVariable("orderId") String orderId) {
        Ensure.that(orderId).isNotNull("00010003");
        BaseOrderDto bod = orderInfoFacade.find(orderId);
        return new Result(bod);
    }

    /**
     * 创建订单
     * @param request
     * @return
     */
    @PostMapping("/create")
    public Result create(OrderInfoRequest request) {
        Ensure.that(request).isNotNull("00000002");
        return new Result(orderInfoFacade.createOrder(DozerUtils.map(request, CarSaleOrderDto.class)));
    }

    /**
     * 更新订单
     * @param request
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestHeader("token") String token, OrderInfoRequest request) {
        Ensure.that(request).isNotNull("00000002");
        if (StringUtil.isNotEmpty(token)) {
            MemberToken memberToken = DozerUtils.map(baseApi.isMemberLogin(token).getResult(),MemberToken.class);
            Ensure.that(memberToken).isNotNull("00001033");
            request.setUpdateUser(memberToken.getId());
        }
        Ensure.that(orderInfoFacade.update(DozerUtils.map(request,BaseOrderDto.class))).isTrue("00010005");
        return new Result();
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/cancel/{orderId}",method = RequestMethod.GET)
    public Result cancel(@RequestHeader("token") String token, @PathVariable("orderId") String orderId) {
        Ensure.that(orderId).isNotNull("00000002");
        BaseOrderDto dto = new BaseOrderDto();
        dto.setOrderId(orderId);
        if (StringUtil.isNotEmpty(token)) {
            MemberToken memberToken = DozerUtils.map(baseApi.isMemberLogin(token).getResult(),MemberToken.class);
            Ensure.that(memberToken).isNotNull("00001033");
            dto.setUpdateUser(memberToken.getId());
        }
        Ensure.that(orderInfoFacade.cancel(dto)).isTrue("00010006");
        return new Result();
    }

    /**
     * 确认支付房车租赁订单
     * @param request
     * @return
     */
    @RequestMapping(value = "/confirm/pay",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result confirmPay(@RequestHeader("token") String token, @RequestBody OrderInfoCarRentRequest request) {
//        Ensure.that(request).isNotNull("00000002");
//        Ensure.that(token).isNotNull("00000002");
//        MemberToken memberToken = DozerUtils.map(baseApi.isMemberLogin(token).getResult(),MemberToken.class);
//        Ensure.that(memberToken).isNotNull("00001033");
//        request.setUpdateUser(memberToken.getId());
        return new Result(orderInfoFacade.confirmPay(DozerUtils.map(request, CarRentOrderDto.class)));
    }

    /**
     * 取消支付，app使用
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/cancel/pay/app/{orderId}",method = RequestMethod.GET)
    public Result cancelPayApp(@RequestHeader("token") String token, @PathVariable("orderId") String orderId) {
//        Ensure.that(orderId).isNotNull("00000002");
//        Ensure.that(token).isNotNull("00000002");
//        MemberToken memberToken = DozerUtils.map(baseApi.isMemberLogin(token).getResult(),MemberToken.class);
//        Ensure.that(memberToken).isNotNull("00001033");
        return new Result(orderInfoFacade.cancelPay(orderId));
    }

    /**
     * 取消支付，服务调用
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/cancel/pay/service",method = RequestMethod.POST)
    public Result cancelPayService(String orderId) {
        Ensure.that(orderId).isNotNull("00000002");
        return new Result(orderInfoFacade.cancelPay(orderId));
    }
}
